// morePhoto.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: -1,
        condition: true,
        active: '',
        show: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var photo_urls = wx.getStorageSync('photo_urls')
        this.setData({
            photo_urls: photo_urls
        })
        var session_key = wx.getStorageSync('session_key');
        wx.request({
                url: 'http://sheying.j8j0.com/api/Upload/add_video',
                data: {
                    session_key: session_key
                },
                success: function(res) {
                    // console.log(res)
                    let cates = []
                    let musics = []
                        // console.log(res.data.data.cate)
                    var Length = res.data.data.cate.length;
                    // console.log(Length)
                    for (var i = 0; i < Length; i++) {
                        cates[i] = res.data.data.cate[i].cat_name
                    }
                    for (var i = 0; i < res.data.data.music.length; i++) {
                        musics[i] = res.data.data.music[i].name
                    }
                    that.setData({
                        cates: cates,
                        musics: musics,
                        cate: res.data.data.cate,
                        music: res.data.data.music
                    })
                }
            })
            // 引入SDK核心类
        var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');

        // 实例化API核心类
        var demo = new QQMapWX({
            key: 'ALWBZ-6WC34-LGBUK-DBH4M-CXPT3-46FUI' // 必填
        });
        demo.reverseGeocoder({
            location: {
                'latitude': 23.129163,
                'longitude': 113.264435
            },
            get_poi: 1,
            success: function(res) {
                that.setData({
                    addresses: res.result.pois
                })
                console.log('res', res)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindpost_photo: function() {
        var that = this;
        var session_key = wx.getStorageSync('session_key');
        var filePath = that.data.photo_urls;
        var day = new Date();
        var time = day.getTime();
        // console.log(time);
        // console.log(that.data.photos_Text)
        var photoArr = [];
        for (let i = 0; i < filePath.length; i++) {
            wx.uploadFile({
                url: 'http://sheying.j8j0.com/api/Upload/uploadPhotos',
                method: "POST",
                filePath: filePath[i],
                name: 'img',
                header: {
                    'content-type': 'multipart/form-data'
                },
                formData: {
                    'session_key': session_key,
                    'name': filePath.length
                },
                success: function(res) {
                    // console.log('res', res)
                    var data = JSON.parse(res.data);
                    // console.log(data)
                    photoArr.push(data.data.img_file)
                        // console.log(photoArr)
                        // wx.switchTab({
                        //     url: '../../index/index'
                        // })
                    that.setData({
                            photoArr: photoArr
                        })
                        // console.log(filePath.length)
                        // console.log(photoArr.length)
                    if (filePath.length === photoArr.length) {
                        var img_file = ''

                        for (var i = 0; i < that.data.photoArr.length; i++) {
                            img_file += that.data.photoArr[i] + ','
                        }
                        // console.log(img_file)
                        that.setData({
                            img_file: img_file
                        })
                        wx.request({
                            url: 'http://sheying.j8j0.com/api/Upload/post_video',
                            method: 'POST',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                                session_key: session_key,
                                img_file: img_file,
                                details: that.data.photos_Text,
                                cat_id: that.data.cat_id
                            },
                            success: function(res) {
                                console.log(res)
                                wx.switchTab({
                                    url: '../../index/index'
                                })
                            }
                        })
                    }
                },
                file: function(res) {
                    // console.log(res)
                    wx.showToast({
                        title: res.data
                    })
                }
            })

        }


    },
    bindphotos_Text: function(e) {
        console.log(e)
        this.setData({
            photos_Text: e.detail.value
        })
    },
    // 视频id获取
    bindPickerChangeCat: function(e) {
        var that = this
        console.log('Cat发送选择改变，携带值为', e.detail.value)
        that.setData({
            index: e.detail.value,
            cat_id: that.data.cate[e.detail.value].id
        })
    },
    bindGuanbi: function() {
        console.log('aaa')
        this.setData({
            active: '',
            condition: false

        })
    },
    bindAddress: function() {
        this.setData({
            active: 'active',
            show: false
        })
    },
    checkAddress: function(e) {
        console.log(e)
        console.log(e.currentTarget.dataset.value)
        this.setData({
            active: '',
            address: e.currentTarget.dataset.value,
            condition: true,
            show: true
        })
    }
})